package com.xiaochong.tir.apiservice.component;

import akka.actor.UntypedActor;
import com.alibaba.fastjson.JSONObject;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.apiservice.component.hbase.CurrencyInfoHbaseComponent;
import com.xiaochong.tir.apiservice.util.SpringContextUtil;
import com.xiaochong.tir.common.data.hbasepo.*;
import com.xiaochong.tir.common.util.AkkaParamUtils;
import com.xiaochong.tir.common.util.CollectionUtil;
import com.xiaochong.tir.common.util.LogTraceKeysUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by wujiaxing on 2018/5/7.
 */
public class AkkaDiagnosisMarketPerformanceIndex extends UntypedActor {


    private CmcDataCalculateComponent cmcDataCalculateComponent =
            (CmcDataCalculateComponent) SpringContextUtil.getBean("cmcDataCalculateComponent");

    private CurrencyInfoHbaseComponent currencyInfoHbaseComponent =
            (CurrencyInfoHbaseComponent) SpringContextUtil.getBean("currencyInfoHbaseComponent");

    @Override
    public void onReceive(Object o) {
        LogTrace.beginTrace("大盘指数、权重计算、行业指数、市场表现计算");
        Object o1 = ((Object[]) o)[0];
        Map<String,Object> map = (Map<String, Object>) o1;
        List<CmcCurrencyInfo> cmcCurrencyInfos =
                (List<CmcCurrencyInfo>) map.get(AkkaParamUtils.CMC_CURRENCY_INFO_LIST);
        CmcCurrencyGlobalInfo cmcCurrencyGlobalInfo =
                (CmcCurrencyGlobalInfo) map.get(AkkaParamUtils.CMC_GLOBAL_INFO);
        Map<String, CmcCurrencyInfo> cmcMap =
                (Map<String, CmcCurrencyInfo>) map.get(AkkaParamUtils.CMC_CURRENCY_INFO_MAP);

        LogTrace.info(LogTraceKeysUtils.AKKA_DIAGNOSIS_MARKET_PERFORMANCE_INDEX_TITLE, LogTraceKeysUtils.DATA,"市场表现相关计算开始");

        BroaderIndex broaderIndex = null;
        try{
            //计算大盘指数
            broaderIndex = cmcDataCalculateComponent.calculateBroaderIndex(cmcCurrencyGlobalInfo);

            LogTrace.info(LogTraceKeysUtils.AKKA_DIAGNOSIS_MARKET_PERFORMANCE_INDEX_TITLE, LogTraceKeysUtils.DATA, broaderIndex != null? JSONObject.toJSONString(broaderIndex):"null");
            if(broaderIndex!=null){
                //判断大盘指数是否计算成功，插入HBASE
                currencyInfoHbaseComponent.insertHbaseBroaderIndex(broaderIndex);
            }else{
                broaderIndex = currencyInfoHbaseComponent.getLastBroaderIndex();
            }
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error(e.getMessage(), e);
        }
        LogTrace.info(LogTraceKeysUtils.AKKA_DIAGNOSIS_MARKET_PERFORMANCE_INDEX_TITLE, LogTraceKeysUtils.DATA, "大盘指数计算完成");

        WeightIndex weightIndex = null;
        try {
            weightIndex = cmcDataCalculateComponent.calculateWeightIndex(cmcMap);
            LogTrace.info(LogTraceKeysUtils.AKKA_DIAGNOSIS_MARKET_PERFORMANCE_INDEX_TITLE, LogTraceKeysUtils.DATA, "权重计算完成");
            if(weightIndex!=null){
                currencyInfoHbaseComponent.insertHbaseWeightIndex(weightIndex);
            }
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error(e.getMessage(), e);
        }

        List<IndustryIndex> industryIndices = null;
        try{
            industryIndices = cmcDataCalculateComponent.calculateIndustryIndex(cmcCurrencyInfos);
            LogTrace.info(LogTraceKeysUtils.AKKA_DIAGNOSIS_MARKET_PERFORMANCE_INDEX_TITLE, LogTraceKeysUtils.DATA, "行业指数计算完成");
            if(CollectionUtil.isNotBlank(industryIndices)){
                currencyInfoHbaseComponent.insertHbaseIndustryIndex(industryIndices);
            }else{
                industryIndices = currencyInfoHbaseComponent.getLastIndustryIndex();
            }
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error(e.getMessage(), e);
        }
        /*if(weightIndex==null || industryIndices==null){
            CalculateTicker calculateTicker = currencyInfoHbaseComponent.getLastCalculateTicker();
            if(weightIndex==null){
                List<WeightIndex> weightIndices = calculateTicker.getWeightIndices();
                if(CollectionUtil.isNotBlank(weightIndices)){
                    weightIndex = weightIndices.get(weightIndices.size()-1);
                }
            }
            if(industryIndices==null){
                Map<Integer, List<IndustryIndex>> industryIndicesMap = calculateTicker.getIndustryIndices();
                if(CollectionUtil.isNotBlank(industryIndicesMap)){
                    industryIndices = new ArrayList<>();
                    for (Integer key:industryIndicesMap.keySet()) {
                        industryIndices.addAll(industryIndicesMap.get(key));
                    }
                }
            }
        }
        currencyInfoHbaseComponent.insertHbaseCalculate(weightIndex,industryIndices);
        */
        List<DiagnosisMarketPerformance> diagnosisMarketPerformances =
                cmcDataCalculateComponent.calculateDiagnosisMarketPerformance(cmcCurrencyInfos, broaderIndex, industryIndices);
        LogTrace.info(LogTraceKeysUtils.AKKA_DIAGNOSIS_MARKET_PERFORMANCE_INDEX_TITLE, LogTraceKeysUtils.DATA, "市场表现计算完成");
        if(diagnosisMarketPerformances!=null && diagnosisMarketPerformances.size()>0){
            cmcDataCalculateComponent.saveDiagnosisMarketPerformance(diagnosisMarketPerformances);
        }
        LogTrace.info(LogTraceKeysUtils.AKKA_DIAGNOSIS_MARKET_PERFORMANCE_INDEX_TITLE, LogTraceKeysUtils.DATA, "整体计算完成");
        LogTrace.endTrace();
    }
}
